package com.happycat.service.impl;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.happycat.Pagination;
import com.happycat.ResponseHeaderModel;
import com.happycat.ResponseModel;
import com.happycat.SysContants;
import com.happycat.config.HappyCatSecurity;
import com.happycat.config.ShortMessage;
import com.happycat.mapper.UserShortMessageMapper;
import com.happycat.mobile.enums.ShortMessageEnum;
import com.happycat.mobile.model.UserShortMessage;
import com.happycat.mobile.service.GenerateIdService;
import com.happycat.mobile.service.UserShortMessageService;
import com.happycat.utils.AesUtils;
import com.happycat.utils.DateUtil;
import com.happycat.utils.ResponseModelUtils;
import com.happycat.utils.SmsUtils;

/**
 * Module: UserShortMessage.java
 * 
 * @author liaoyong
 * @since JDK 1.8
 * @version 1.0
 * @date 2020-07-24
 * @Descriptions:
 */
@Service
public class UserShortMessageServiceImpl extends ServiceImpl<UserShortMessageMapper, UserShortMessage>
		implements UserShortMessageService {

	@Autowired
	private GenerateIdService generateIdService;
	@Autowired
	private HappyCatSecurity happyCatSecurity;

	public ResponseModel<UserShortMessage> selectOne(UserShortMessage userShortMessage) {
		Wrapper<UserShortMessage> queryWrapper = new QueryWrapper<>(userShortMessage);
		ResponseModel<UserShortMessage> responseModel = new ResponseModel<>();
		responseModel.setCode(SysContants.SYS_SUCCESS_CODE);
		responseModel.setMessage("查询成功");
		responseModel.setData(getOne(queryWrapper));
		return responseModel;

	}

	public ResponseHeaderModel add(UserShortMessage userShortMessage) {
		ResponseHeaderModel responseHeaderModel = new ResponseHeaderModel();

		boolean result = save(userShortMessage);
		if (result) {
			responseHeaderModel.setCode(SysContants.SYS_SUCCESS_CODE);
			responseHeaderModel.setMessage("新增成功");
		} else {
			responseHeaderModel.setCode(SysContants.SYS_FAIL_CODE);
			responseHeaderModel.setMessage("新增失败");
		}
		return responseHeaderModel;
	}

	public ResponseHeaderModel modify(UserShortMessage userShortMessage) {
		ResponseHeaderModel responseHeaderModel = new ResponseHeaderModel();

		boolean result = updateById(userShortMessage);
		if (result) {
			responseHeaderModel.setCode(SysContants.SYS_SUCCESS_CODE);
			responseHeaderModel.setMessage("修改成功");
		} else {
			responseHeaderModel.setCode(SysContants.SYS_FAIL_CODE);
			responseHeaderModel.setMessage("修改失败");
		}
		return responseHeaderModel;
	}

	public ResponseModel<Page<UserShortMessage>> pageList(Pagination<UserShortMessage>page) {
		ResponseModel<Page<UserShortMessage>> responseModel = new ResponseModel<>();
		responseModel.setCode(SysContants.SYS_SUCCESS_CODE);
		responseModel.setMessage("查询成功");
		responseModel.setData(page(page,new QueryWrapper<UserShortMessage>(page.getEntity())));
		return responseModel;
	}

	@Override
	public ResponseModel<String> sendMessage(UserShortMessage userShortMessage)  {
		try {			
			SmsUtils.sendMessage(userShortMessage);
			save(userShortMessage);
			return ResponseModelUtils.success("发送成功！", "");
		} catch (ServerException e) {
			e.printStackTrace();
			ResponseModelUtils.fail("短信云服务器异常", "");
		} catch (ClientException e) {
			e.printStackTrace();
			ResponseModelUtils.fail("系统异常！", "");
		}
		return ResponseModelUtils.fail("系统异常！", "");

	}

	@Override
	public ResponseModel<String> verifyCode(UserShortMessage userShortMessage) {
		try {
			userShortMessage.setPhone(AesUtils.decrypt(userShortMessage.getPhone(), happyCatSecurity.getAesKey()));
		}catch(Exception e) {
			return ResponseModelUtils.fail("手机号不正确", "");
		}
		if(StringUtils.isBlank(userShortMessage.getTemplateCode())) {
			return ResponseModelUtils.fail("短信模板不能为空！", "");
		}
		//查询五分钟内是否有发送过短信
		userShortMessage.setTemplateCode(ShortMessageEnum.getShortMessageEnumName(userShortMessage.getTemplateCode()));
		userShortMessage.setStatus(SysContants.NO);//未使用	
		QueryWrapper<UserShortMessage> userShortMessageWrapper = new QueryWrapper<UserShortMessage>(userShortMessage);
		userShortMessageWrapper.gt("create_time",DateUtil.addDate(new Date(), 0, 0, 0, 0, -5, 0, 0));
		UserShortMessage userShortMessageResult = getOne(userShortMessageWrapper);
		if(userShortMessageResult != null) {
			return ResponseModelUtils.fail("验证码已发送，五分钟内有效，请尽快使用", "");
		}

		userShortMessage.setSignName(SysContants.SIGN_NAME);
		userShortMessage.setMessage(SmsUtils.getVerifyCode());//设置验证码
		userShortMessage.setCreateTime(new Date());
		userShortMessage.setId(generateIdService.nextDefaultSerialNumber());
			
		/*
		 * try { SmsUtils.sendMessage(userShortMessage); save(userShortMessage); return
		 * ResponseModelUtils.success("发送成功！", userShortMessage.getMessage()); } catch
		 * (ServerException e) { e.printStackTrace();
		 * ResponseModelUtils.fail("短信云服务器异常", ""); } catch (ClientException e) {
		 * e.printStackTrace(); ResponseModelUtils.fail("系统异常！", ""); }
		 */
		//SmsUtils.sendMessage(userShortMessage);
		save(userShortMessage);
		return ResponseModelUtils.success("发送成功！", userShortMessage.getMessage());
	}

	@Override
	public ResponseModel<String> checkVerifyCode(UserShortMessage userShortMessage) {
		try {
			userShortMessage.setPhone(AesUtils.decrypt(userShortMessage.getPhone(), happyCatSecurity.getAesKey()));
		}catch(Exception e) {
			return ResponseModelUtils.fail("手机号不正确", "");
		}
		if (StringUtils.isBlank(userShortMessage.getMessage())) {
			return ResponseModelUtils.fail("请输入验证码！", null);
		}
		// 登录认证
		userShortMessage.setStatus(SysContants.NO);
		userShortMessage.setTemplateCode(ShortMessageEnum.getShortMessageEnumName(userShortMessage.getTemplateCode()));
		QueryWrapper<UserShortMessage> shortMessageWrapper = new QueryWrapper<>(userShortMessage);
		shortMessageWrapper.gt("create_time",DateUtil.addDate(new Date(), 0, 0, 0, 0, -5, 0, 0));
		UserShortMessage userShortMessageResult = getOne(shortMessageWrapper);
		if (userShortMessageResult == null) {
			return ResponseModelUtils.fail("验证不正确", null);
		}
		UserShortMessage data = new UserShortMessage();
		data.setId(userShortMessageResult.getId());
		data.setStatus(SysContants.YES);
		// 更新验证码状态
		updateById(data);// 更新验证码状态
		return ResponseModelUtils.success("验证成功！", null);
	}
}
